package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.LabelOptionDTO;
import com.jyh.blog.dto.ResourceDTO;
import com.jyh.blog.service.ResourceService;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.AnonymousResourceVO;
import com.jyh.blog.vo.ConditionVO;
import com.jyh.blog.vo.ResourceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.jyh.blog.constant.OptTypeConst.*;

@Api(tags = "资源管理")
@RestController
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * 获取资源列表
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "获取资源列表")
    @GetMapping("admin/resources")
    public Result<List<ResourceDTO>> resourceList(ConditionVO conditionVO){
        return Result.ok(resourceService.resourceList(conditionVO));
    }

    /**
     * 添加或修改资源
     * @param resourceVO
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改资源")
    @PostMapping("admin/resources")
    public Result<String> saveOrUpdateResource(@Valid @RequestBody ResourceVO resourceVO){
        resourceService.saveOrUpdateResource(resourceVO);
        return Result.ok();
    }

    /**
     * 删除资源
     * @param id
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "通过id删除资源")
    @DeleteMapping("admin/resources/{id}")
    public Result<String> deleteResourceById(@PathVariable String id){
        resourceService.deleteResourceById(id);
        return Result.ok();
    }

    /**
     * 开关匿名访问
     * @param anonymousResourceVO
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "开关匿名访问")
    @PutMapping("admin/resources")
    public Result<String> changAnonymous(@Valid @RequestBody AnonymousResourceVO anonymousResourceVO){
        resourceService.changAnonymous(anonymousResourceVO);
        return Result.ok();
    }

    /**
     * 获取角色资源列表
     * 权限管理中用
     * @return
     */
    @ApiOperation(value = "获取角色资源列表")
    @GetMapping("admin/resources/roles")
    public Result<List<LabelOptionDTO>> roleResourceList(){
        return Result.ok(resourceService.roleResourceList());
    }

    /**
     * swagger导入资源
     * @return
     */
    @ApiOperation(value = "swagger导入资源")
    @GetMapping("admin/resource/import")
    public Result importSwagger(){
        resourceService.importSwagger();
        return Result.ok();
    }
}
